<template>
	<view>
		<view class="head">
			<view class="head-text" @tap="openshare(0, 'center')">添加新的员工</view>
			<!-- <FacInfoModify @factory_info="factory_info" :factory_info_data="factory_info_data" :factory_id="factory_id" :modify="modify" :scrollH="scrollH" class="mt-2" /> -->
		</view>

		<!-- <facInfo :factory_info_data="factory_info_data" class="pl-5 pr-5 line-h-md"/> -->
			
		<!-- 员工列表 -->
		<staff-list @staff_list="staff_list" :staff_list_data="staff_list_data" :factory_id="factory_id" :activity_id="activity_id" :scrollH="scrollH" :status="status" />
		
		<fac-tab-bar :pagePath="'/pages/factory/management'" :activity_id="activity_id" :factory_id="factory_id" />

		<view class="top" :style="{'display':(flag === true? 'block':'none')}">
			<uni-icons class="topc" type="arrowthinup" size="30" @click="top"></uni-icons>
		</view>
		
		<canvas class="canvas" canvas-id="qrcode" />
		<openAlert :BgColor="BgColor" closeBtn ref="openAlertshare" :AlertClass="AlertClass" :AlertPosition="AlertPosition">
			<view class="d-flex flex-column a-center">
				<text class="sharetext">长按图片,分享好友</text>
				<image :src="haibao_url" class="shareimg"></image>
			</view>
		</openAlert>

	</view>
</template>

<script>
	import uQRCode from '../../common/uqrcode.js';
	require('../../components/poster/poster_maker.js');
	import staffList from '@/components/factory/factory_staff_list.vue'
	import facTabBar from '@/components/factory/fac_tabbar.vue'
	import uniIcons from '../../components/common/uni-icons/uni-icons.vue'

	export default {
		components: {
			staffList,
			facTabBar,
			uniIcons,
		},
		data() {
			return {
				scrollH: 100,
				BgColor: '#838383',
				haibao_url: "",
				activity_detail_data: {},
				factory_info_data: {},
				AlertClass: 0,
				AlertPosition: '',
				modify: {
					boss_name: '',
					boss_tel: '',
					name: '',
					address: '',
					phone: '',
					head_url: '',
					code_url: ''
				},
				staff_list_data: [],
				factory_id: 0, //
				activity_id: '',
				factory_name: '',
				page: 1,
				status: 'nomore',
				flag: false,
				haibao_id: 0,
				factory_status: 0,
			};
		},
		onLoad(e) {
			// console.log(e);
			this.$comm.appLogin(user_info=>{
				this.activity_id = e.activity_id;
				this.factory_info(e.activity_id);
				this.activity_detail()
			})
		},
		// onShow() {
		// 	console.log('onshow')
		// 	this.loaddata()
		// 	this.staff_list()
		// },
		onPullDownRefresh() {
			this.loaddata()
		},
		onReachBottom() {
		},
		methods: {
			//获取商家信息
			factory_info(id) {
				var id = this.activity_id;
				// console.log(this.$comm.app_config.share_url)
				this.$comm.request({
					url: 'api/my_factory_info?activity_id=' + id,
					method: 'GET',
					data: {
						activity_id: id
					},
					success: res => {
						this.factory_id = res.data.result.id;
						this.factory_info_data = res.data.result;
						console.log(res, '商家信息',this.factory_info_data,this.factory_id);
						this.modify.boss_name = res.data.result.boss_name;
						this.modify.boss_tel = res.data.result.boss_tel;
						this.modify.name = res.data.result.name;
						this.modify.phone = res.data.result.tel;
						this.modify.address = res.data.result.address;
						this.modify.head_url = res.data.result.head_url;
						this.modify.code_url = res.data.result.code_url;
						this.factory_name = res.data.result.name;
						this.factory_status = res.data.result.status
						this.staff_list();
					}
				});
			},

			//获取员工列表
			staff_list(id) {
				var id = this.factory_id;
				this.$comm.request({
					url: 'api/staff_list',
					method: 'GET',
					data: {
						fac_id: id,
						activity_id: this.activity_id,
					},
					success: res => {
						console.log('xxx');
						this.staff_list_data = res.data.result;
						console.log(this.staff_list_data)
					}
				});
			},
			
			// 下拉刷新
			loaddata() {
				this.page = 1
				this.status = 'more'
				this.staff_list_data = []
				this.staff_list()
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 1000);
			},

			//回到顶部
			top() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},
			onPageScroll(e) { //根据距离顶部距离是否显示回到顶部按钮
				if (e.scrollTop > 600) { //当距离大于600时显示回到顶部按钮
					this.flag = true
				} else { //当距离小于600时显示回到顶部按钮
					this.flag = false
				}
			},

			get_scrollH() {
				//获取可视区域高度
				uni.getSystemInfo({
					success: (res) => {
						this.scrollH = res.windowHeight - uni.upx2px(250)
						// console.log(res.windowHeight, '可视区域高度',this.scrollH)
					}
				})
			},

			//获取活动详情
			activity_detail(id) {
				this.$comm.request({
					url: 'api/activity_index?activity_id=' + 80,
					method: 'GET',
					success: res => {
						console.log(res, '活动详情');
						this.activity_detail_data = res.data.result;
					}
				});
			},
			
			//打开添加员工页面
			openshare(Class, Position) {
				if (this.factory_info_data.status == 0) {
					uni.showToast({
						title: '工厂审核未通过，暂时不能添加员工！',
						icon: 'none',
						defaults: 2000
					})
					return
				}
				this.$nextTick(function() {
					this.AlertClass = Class;
					this.AlertPosition = Position;
					this.$nextTick(function() {
						this.make();
						uni.showToast({
							icon: 'loading',
							duration: 450,
							title: '二维码生成中...',
						});
						setTimeout(() => {
							this.$refs.openAlertshare.Show();
						}, 500);
					});
				});
			},

			//制作二维码
			make(id) {
				var id = this.factory_id; //
				let fname = this.factory_name
				let atitle = this.activity_detail_data.title
				let rukou = '商家员工入口'
				uQRCode.make({
					canvasId: 'qrcode',
					componentInstance: this,
					text: this.$comm.app_config.share_url + '/staff/staff_sign?factory_id=' + id + '&activity_id=' + this.activity_id,
					// text: 'http://192.168.0.190:8081/bm/#/pages/staff/staff_sign?factory_id=' + id + '&activity_id=' + this.activity_id,
					size: 230,
					backgroundColor: '#FFFFFF',
					foregroundColor: '#000000',
					fileType: 'jpg',
					correctLevel: uQRCode.defaults.correctLevel,
					success: res => {
						// console.log(id);
						// postMaker(66, [fname + '(' + atitle + ')', res, rukou], 500, url => {
						postMaker(66, [fname, res, rukou], 500, url => {
							this.haibao_url = url;
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import '@/common/main.css';
	@import  '@/common/zcm-main.css';
	
	page {
		background-color: #f1f1f1;
		font-size: 30rpx;
	}

	.head {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx 50rpx 10rpx 50rpx;

		.head-text {
			color: #ffffff;
			line-height: 2.5;
			background-color: #475dfc;
			width: 400rpx;
			text-align: center;
			border-radius: 50rpx;
		}
	}

	.sharetext {
		color: #fff;
		font-size: 35rpx;
		margin-bottom: 10rpx;
	}

	.shareimg {
		border-radius: 20rpx;
		height: 950rpx;
		width: 580rpx;
	}

	.canvas {
		// background-color: #4CD964;
		width: 500rpx;
		height: 500rpx;
		position: fixed;
		right: 100vw;
		bottom: 100vh;
		z-index: -9999;
		opacity: 0;
	}

	.top {
		position: relative;
		display: none;
		/* 先将元素隐藏 */
	}

	.topc {
		position: fixed;
		right: 0;
		// background: #F0F0F0;
		bottom: 120rpx;
		right: 30rpx;
		background-color: #FFF;
		border: solid 2rpx #475dfc;
		border-radius: 100rpx;
		height: 80rpx;
		width: 80rpx;
		line-height: 80rpx;
	}
</style>
